﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;
using Model;

namespace DAL
{
    public class DPhoneUser
    {
         public PhoneUser GetModelByPhoneNumber(string phoneNumber)
         {
             StringBuilder strSql = new StringBuilder();
             strSql.Append("SELECT * from tphoneuser ");
             strSql.Append("where PhoneNumber=@PhoneNumber");
             MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.String)
             };
             parameters[0].Value = phoneNumber;
             DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
             if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
                 return null;
             PhoneUser model = new PhoneUser();
             model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
             model.PhoneNumber = ds.Tables[0].Rows[0]["PhoneNumber"].ToString();
             model.Imei = ds.Tables[0].Rows[0]["Imei"].ToString();
             model.RegistTime = DateTime.Parse(ds.Tables[0].Rows[0]["RegistTime"].ToString());
             model.LastLoginTime = DateTime.Parse(ds.Tables[0].Rows[0]["LastLoginTime"].ToString());
             return model;
         }

         /// <summary>
         /// 增加一条数据
         /// </summary>
         public bool Add(PhoneUser model)
         {
             StringBuilder strSql = new StringBuilder();
             strSql.Append("insert into tphoneuser(");
             strSql.Append("PhoneNumber,IMEI,RegistTime");
             strSql.Append(",LastLoginTime,LoginCount)");             
             strSql.Append(" values (");
             strSql.Append("@PhoneNumber,@IMEI,@RegistTime");
             strSql.Append(",@LastLoginTime,@LoginCount)");
             MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,20),
					new MySqlParameter("@IMEI", MySqlDbType.VarChar,20),
					new MySqlParameter("@RegistTime", MySqlDbType.DateTime),
                    new MySqlParameter("@LastLoginTime", MySqlDbType.DateTime),
                    new MySqlParameter("@LoginCount", MySqlDbType.Int32)
				 };
             parameters[0].Value = model.PhoneNumber;
             parameters[1].Value = model.Imei;
             parameters[2].Value = model.RegistTime;
             parameters[3].Value = model.LastLoginTime;
             parameters[4].Value = 1;
             int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
             if (rows > 0)
             {
                 return true;
             }
             else
             {
                 return false;
             }
         }

         /// <summary>
         /// 更新一条数据
         /// </summary>
         public bool Update(PhoneUser model)
         {
             StringBuilder strSql = new StringBuilder();
             strSql.Append("update tphoneuser set ");             
             strSql.Append("IMEI=@IMEI,");              
             strSql.Append("LastLoginTime=@LastLoginTime,");
             strSql.Append("LoginCount=LoginCount+1");
             strSql.Append(" where PhoneNumber=@PhoneNumber");
             MySqlParameter[] parameters = {					
					new MySqlParameter("@IMEI", MySqlDbType.VarChar,20),                    
                    new MySqlParameter("@LastLoginTime", MySqlDbType.DateTime),
                    new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,20)};
             parameters[0].Value = model.Imei;
             parameters[1].Value = DateTime.Now;
             parameters[2].Value = model.PhoneNumber;              
             int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
             if (rows > 0)
             {
                 return true;
             }
             else
             {
                 return false;
             }
         }


    }
}
